package com.ebring.shareEducation.controller;

import com.ebring.shareEducation.bean.Institution;
import com.ebring.shareEducation.bean.InstitutionCode;
import com.ebring.shareEducation.bean.admin.InstitutionAll;
import com.ebring.shareEducation.service.InstitutionService;
import com.ebring.shareEducation.utils.RspMsg;
import com.github.pagehelper.PageHelper;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/institution")
@Api(value = "部门对象管理" ,tags = {"部门对象管理接口"})
@CrossOrigin(origins = "http://wangliang.vipgz1.idcfengye.com", maxAge = 3600)
public class InstitutionController {

	@Autowired
	private InstitutionService institutionService;

	/**
	 * 添加下级和一级机构
	 * @param institution
	 * @param
	 * @return
	 */
	@PostMapping("/insert")
	@ApiOperation(value="添加下级和一级部门", notes="添加下级和一级部门")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType="query",name = "Institution", value = "部门对象institution，包含institutionId", required = true, dataType = "Institution")
	})
	public RspMsg<Institution> insertUser(Institution institution) {
		return institutionService.insertInstitution(institution);
	}

	/**
	 * 添加平级机构
	 * @param institution
	 * @return
	 */
	@PostMapping("/insertSame")
	@ApiOperation(value="添加平级部门", notes="添加平级部门")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType="query",name = "Institution", value = "部门对象institution", required = true, dataType = "Institution")
	})
	public RspMsg<Institution> insertSameInstitution(Institution institution){
		return institutionService.insertSameInstitution(institution);
	}

	/**
	 * 查询所有机构级别
	 * @return
	 */
	@GetMapping("/selectlevel")
	@ApiOperation(value="查询所有部门级别", notes="查询所有部门级别")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType="query",name = "parentId", value = "部门编号", required = true, dataType = "String")
	})
	public RspMsg<List<InstitutionCode>> selectlevelAll(String parentId) {
		return institutionService.insertInstitutionCode(parentId);
	}



	/**
	 * 删除机构
	 *
	 * @param institutionId
	 *            机构Id
	 * @return 删除成功的数量
	 */
	@GetMapping("/delete")
	@ApiOperation(value="删除部门", notes="删除部门")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType="query",name = "institutionId", value = "部门编号", required = true, dataType = "String[]")
	})
	public RspMsg<Institution> DeleteUser(String[] institutionId) {
		return this.institutionService.deleteInstitution(institutionId);
	}

	/**
	 * 更新用户
	 *
	 * @param institution
	 *            institution对象
	 * @return 更新信息
	 */
	@PostMapping("/update")
	@ApiOperation(value="更新用户", notes="更新用户")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType="query",name = "Institution", value = "部门对象", required = true, dataType = "Institution")
	})
	public RspMsg<Institution> updateInstitution(Institution institution) {
		return institutionService.updateInstitution(institution);
	}

	/**
	 * 模糊查询机构
	 *
	 * @param institutionId
	 *            机构编号
	 * @param institutionName
	 *            机构名称
	 * @return 查询结果集合
	 */
	@PostMapping("/select")
	@ApiOperation(value="模糊查询部门", notes="模糊查询部门")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType="query",name = "institutionId", value = "部门编号", required = true, dataType = "String"),
			@ApiImplicitParam(paramType="query",name = "parentId", value = "上级部门编号", required = true, dataType = "String"),
			@ApiImplicitParam(paramType="query",name = "institutionlevel", value = "部门级别", required = true, dataType = "String"),
			@ApiImplicitParam(paramType="query",name = "institutionName", value = "部门名称", required = true, dataType = "String"),
			@ApiImplicitParam(paramType="query",name = "pageNum", value = "页数", required = true, dataType = "Integer"),
			@ApiImplicitParam(paramType="query",name = "pageSize", value = "总页数", required = true, dataType = "Integer"),
	})
	public RspMsg<List<InstitutionAll>> select(String institutionId,
											   String parentId,
											   String institutionlevel,
											   String institutionName,
											   @RequestParam(name = "pageNum", required = false, defaultValue = "1")Integer pageNum,
											   @RequestParam(name = "pageSize", required = false, defaultValue = "5")Integer pageSize) {

		PageHelper.startPage(pageNum, pageSize);
		return institutionService.select(institutionId, institutionName,parentId,institutionlevel);
	}

	/**
	 *根据用户机构id查询机构
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@GetMapping("/selectAll")
	@ApiOperation(value="根据用户部门id查询机构", notes="根据用户部门id查询机构")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType="query",name = "institutionId", value = "部门编号", required = true, dataType = "String"),
			@ApiImplicitParam(paramType="query",name = "pageNum", value = "页数", required = true, dataType = "Integer"),
			@ApiImplicitParam(paramType="query",name = "pageSize", value = "总页数", required = true, dataType = "Integer"),
	})
	public RspMsg<List<InstitutionAll>> selectAll(String institutionId,
												  @RequestParam(name = "pageNum", required = false, defaultValue = "1") Integer pageNum,
												  @RequestParam(name = "pageSize", required = false, defaultValue = "5") Integer pageSize) {
		PageHelper.startPage( pageNum, pageSize);
		return institutionService.selectAll(institutionId);
	}

	/**
	 * 查询下级机构
	 *
	 * @param institutionId 机构编号
	 * @return 查询结果集合
	/* *//*
	@PostMapping("/getLower")
	public RspMsg<Map> getLower(String instlevel,
								@RequestParam(name = "pageNum", required = false, defaultValue = "1") Integer pageNum,
								@RequestParam(name = "pageSize", required = false, defaultValue = "5") Integer pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		return institutionService.getLower(instlevel);
	}*/

	@PostMapping("/getLower")
	@ApiOperation(value="查询下级部门不包含分页", notes="查询下级部门不包含分页")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType="query",name = "parentId", value = "上级部门编号", required = true, dataType = "String"),

	})
	public RspMsg<Map> getLower(String parentId) {
	/*	PageHelper.startPage(pageNum, pageSize);*/
		return institutionService.getLower(parentId);
	}

	@PostMapping("/getLower1")
	@ApiOperation(value="查询下级部门包含分页", notes="查询下级部门包含分页")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType="query",name = "institutionId", value = "部门编号", required = true, dataType = "String"),
			@ApiImplicitParam(paramType="query",name = "pageNum", value = "页数", required = true, dataType = "Integer"),
			@ApiImplicitParam(paramType="query",name = "pageSize", value = "总页数", required = true, dataType = "Integer"),
	})
	public RspMsg<Map> getLower1(String institutionId,
								 @RequestParam(name = "pageNum", required = false, defaultValue = "1") Integer pageNum,
								 @RequestParam(name = "pageSize", required = false, defaultValue = "5") Integer pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		return institutionService.getLower1(institutionId);
	}

	/**
	 * 查询上级机构
	 *
	 * @param institutionId
	 *            机构编号
	 * @return 查询结果
	 */
	@PostMapping("/getParent")
	@ApiOperation(value="查询上级部门", notes="查询上级部门")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType="query",name = "institutionId", value = "部门编号", required = true, dataType = "String"),
			@ApiImplicitParam(paramType="query",name = "pageNum", value = "页数", required = true, dataType = "Integer"),
			@ApiImplicitParam(paramType="query",name = "pageSize", value = "总页数", required = true, dataType = "Integer"),
	})
	public RspMsg<InstitutionAll> getParent(String institutionId,@RequestParam(name = "pageNum", required = false, defaultValue = "1") Integer pageNum,
											@RequestParam(name = "pageSize", required = false, defaultValue = "5") Integer pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		return institutionService.getParent(institutionId);
	}

	/**
	 * 查询总行一级机构
	 */

	@GetMapping("/selectOne")
	@ApiOperation(value="查询一级部门", notes="查询一级部门")
	public RspMsg<List<Institution>> selectOne() {
		return institutionService.selectOne();
	}
}
